package main

import (
	"bar/core"
	"math/rand"
	"strconv"
	"sync"
	"time"
)

var wg sync.WaitGroup

func init() {
	core.MaxNum = 10
}

func main() {
	core.InitBar(10, core.LineNumChan)
	wg.Add(1)
	go func() {
		defer wg.Done()
		ftStr := "\r[%-50s]%3d%%  %8.2f%s/%.2f%s %.2fs %s"
		core.DisplayBar(core.MgCh, "#", ftStr)
	}()

	for k := 0; k <= 100; k++ {
		time.Sleep(50 * time.Millisecond)
		// 这里i是测试用，可以是文件夹路径，等区别其他行数据的数据
		i := rand.Intn(core.MaxNum) + 1
		_, ok := core.LineMsg[strconv.Itoa(i)]
		if !ok {
			select {
			case lineNum := <-core.LineNumChan:
				core.LineMsg[strconv.Itoa(i)] = lineNum + 1
			default:
			}
			ms := new(core.Msg)
			ms.Line = core.LineMsg[strconv.Itoa(i)]
			ms.Max = 100
			ms.Size = k
			ms.Info = strconv.Itoa(i)
			if ms.Size == ms.Max {
				ms.Rs = true
			}
			core.MgCh <- *ms
			continue
		}
		ms := new(core.Msg)
		ms.Line = core.LineMsg[strconv.Itoa(i)]
		ms.Max = 100
		ms.Size = k
		ms.Info = strconv.Itoa(i)
		if ms.Size == ms.Max {
			ms.Rs = true
		}
		core.MgCh <- *ms
	}
	wg.Wait()
}
